package com.kuang.controller;

import com.kuang.dao.DepartmentDao;
import com.kuang.dao.EmployeeDao;
import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

/**
 * @author FHL
 */
@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;
    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }
    @GetMapping("/emp")
    public String toAddPage(Model model){
        Collection<Department> departments = departmentDao.getDepartments();
        //查询出所在的部门名称
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    //两个都是“emp”但是一个是get，一个是post
    @PostMapping("/emp")
    public String add(Employee employee){
employeeDao.add(employee);
        System.out.println(employee);
return "redirect:/emps";
    }
    //修改员工信息
    //去到员工的修改页面
    @GetMapping("/emp/{id}")
    public String toUpdate(@PathVariable("id")Integer id,Model model){
        //查出原来的数据
        Employee employee = employeeDao.getEmployeeById(id);
        model.addAttribute("emp",employee);
        //查询出所在的部门名称
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/update";

    }
    //修改提交
    @PostMapping("/updateEmp")
    public String updateEmP(Employee employee){
employeeDao.add(employee);
return "redirect:/emps";
    }
    //删除员工
@GetMapping("/delemp/{id}")
    public String delete(@PathVariable("id")int id){
        employeeDao.delete(id);
        return "redirect:/emps";
    }

}
